package com.buka.book.controller;

import com.buka.book.entity.Book;
import com.buka.book.service.BookService;
import com.buka.common.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/update")
public class UpdateServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bookName = req.getParameter("bookName");
        Integer count = Integer.parseInt(req.getParameter("count"));
        Integer id = Integer.parseInt(req.getParameter("id"));

        Book book = new Book(id, bookName, count);

        int i = new BookService().updateBook(book);

        if (i > 0) {
            resp.getWriter().write(Result.success(null));
        }else{
            resp.getWriter().write(Result.error("保存失败"));
        }

    }
}
